package com.dq.service.record.impl;

import com.dq.exception.customException.AdminException;
import com.dq.controller.api.merchant.in.query.CusssSubscribeQuery;
import com.dq.domain.subscribe.Subscribe;
import com.dq.domain.org.Org;
import com.dq.domain.sys.user.user.WxUser;
import com.dq.repository.AllRepository;
import com.dq.service.record.CusSubscribeService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.util.List;

/**
 * @author liuhui
 * @date 2019/11/25 0025 14:04:23
 */
@Service
public class CusSubscribeServiceImpl extends AllRepository implements CusSubscribeService {

    @PersistenceContext
    private EntityManager entityManager;

    @Override
    public List<Subscribe> findByWhere(CusssSubscribeQuery cusSubscribeQuery)throws AdminException {
        StringBuilder hql = new StringBuilder("select u from Subscribe u where u.deleted=false");
        Query q = createWhereHql(cusSubscribeQuery,hql);
        q.setFirstResult((cusSubscribeQuery.getPage()-1)*cusSubscribeQuery.getSize());
        q.setMaxResults(cusSubscribeQuery.getSize());
        List<Subscribe> subscribes = q.getResultList();
        return subscribes;
    }

    @Override
    public Long countByWhere(CusssSubscribeQuery cusSubscribeQuery)throws AdminException{
        StringBuilder hql = new StringBuilder("select count(u) from Subscribe u where u.deleted=false");
        Query q = createWhereHql(cusSubscribeQuery,hql);
        return new Long((long) q.getResultList().get(0));
    }

    public Query createWhereHql(CusssSubscribeQuery cusSubscribeQuery,StringBuilder hql){
        if(cusSubscribeQuery.getId() != null){
            hql.append(" and u.wxUser =:wxUser");
        }
        if(null!=cusSubscribeQuery.getSubscribeStatus()){
            hql.append(" and u.subscribeStatus =:status");
        }
        if(null!=cusSubscribeQuery.getBeginTime()){
            hql.append(" and u.beginTime >=:beginTime");
        }
        if(null!=cusSubscribeQuery.getEndTime()){
            hql.append(" and u.beginTime <=:endTime");
        }
        if(StringUtils.isNotEmpty(cusSubscribeQuery.getStaffUserName())){
            hql.append(" and u.technicianUser.userRealcName like:userRealcName");
        }
        if(null!=cusSubscribeQuery.getOrgId()){
            hql.append(" and u.sysDept =:org");
        }
        if(StringUtils.isNotEmpty(cusSubscribeQuery.getWxUserName())){
            hql.append(" and u.wxUser.userRealcName like:wxUserName");
        }
        if(null!=cusSubscribeQuery.getStaffUserId()){
            hql.append(" and u.technicianUser.id =:technicianUser");
        }
        hql.append(" ORDER BY u.createTime desc");
        Query query = entityManager.createQuery(hql.toString());
        if(cusSubscribeQuery.getId() != null){
            query.setParameter("wxUser",dao.get(WxUser.class,cusSubscribeQuery.getId()));
        }
        if(null!=cusSubscribeQuery.getSubscribeStatus()){
            query.setParameter("status",cusSubscribeQuery.getSubscribeStatus());
        }
        if(null!=cusSubscribeQuery.getBeginTime()){
            query.setParameter("beginTime",cusSubscribeQuery.getBeginTime());
        }
        if(null!=cusSubscribeQuery.getEndTime()){
            query.setParameter("endTime",cusSubscribeQuery.getEndTime());
        }
        if(StringUtils.isNotEmpty(cusSubscribeQuery.getStaffUserName())){
            query.setParameter("userRealcName","%"+cusSubscribeQuery.getStaffUserName()+"%");
        }
        if(null!=cusSubscribeQuery.getOrgId()){
            Org org = dao.get(Org.class,cusSubscribeQuery.getOrgId());
            query.setParameter("org",org);
        }
        if(StringUtils.isNotEmpty(cusSubscribeQuery.getWxUserName())){
            query.setParameter("wxUserName",cusSubscribeQuery.getWxUserName());
        }
        if(null!=cusSubscribeQuery.getStaffUserId()){
            query.setParameter("technicianUser",cusSubscribeQuery.getStaffUserId());
        }
        return query;
    }
}
